package com.changgou.system.service.impl;

import com.changgou.system.dao.AdminMapper;
import com.changgou.system.pojo.Admin;
import com.changgou.system.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    public List<Admin> findList() {
        return adminMapper.selectAll();
    }

    @Override
    public Admin findById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void add(Admin admin) {
        adminMapper.insertSelective(admin);
    }

    @Override
    @Transactional
    public void update(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    @Transactional
    public void delById(Integer id) {
        adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean login(Admin admin) {
        Admin admin1 = new Admin();
        admin1.setLoginName(admin.getLoginName());
        Admin result = adminMapper.selectOne(admin1);
        if (result == null) {
            return false;
        } else {
            return BCrypt.checkpw(admin.getPassword(), result.getPassword());
        }
    }

}
